import { createReducer } from 'reduxsauce'
import { REHYDRATE } from 'redux-persist/es/constants'
import {
  GET_TODAY_PAIN_LOG,
  GET_TODAY_STEP,
  SAVE_WECHAT_STEP_RECORDS,
  GET_LASTEST_EXPIRED_PLAN, GET_LOGS_BY_DATE_BUCKET, DELETE_LOG, GET_MONTH_LOG
} from '@ACTIONS/dataCenter'
import { StepRecordITF } from '@INTERFACES/DataCenter'
import { DataCenterStateITF } from '@REDUXITF/dataCenter'
import { LOGOUT_USER } from '@ACTIONS/auth'
import { CREATE_PAIN_LOG } from '@ACTIONS/patient'

const INITIAL_STATE: DataCenterStateITF = {
  todayStepRecord: null,
  todayPainLog: null,
  reportData: null,
  painLogs: [],
  monthPainLogs: [],
}

const rehydrate = (state: DataCenterStateITF, { payload }: { payload: { dataCenter: DataCenterStateITF } }) => {
  if (payload && payload.dataCenter) {
    return { ...INITIAL_STATE, ...payload.dataCenter }
  }
  return { ...state }
}

export default createReducer(INITIAL_STATE, {
  [REHYDRATE]: rehydrate,
  [SAVE_WECHAT_STEP_RECORDS.SUCCESS]: (state, { data }: { data: StepRecordITF }) => ({
    ...state,
    todayStepRecord: data
  }),
  [GET_TODAY_STEP.SUCCESS]: (state, { data }: { data: StepRecordITF }) => ({
    ...state,
    todayStepRecord: data
  }),
  [GET_TODAY_PAIN_LOG.SUCCESS]: (state, { data }) => ({
    ...state,
    todayPainLog: data
  }),
  [GET_LOGS_BY_DATE_BUCKET.SUCCESS]: (state, { data }) => ({
    ...state,
    painLogs: data
  }),
  [DELETE_LOG.SUCCESS]: (state, { logId }) => ({
    ...state,
    painLogs: state.painLogs?.filter(item => item.PainLogId !== logId) || []
  }),
  [GET_MONTH_LOG.SUCCESS]: (state, { data }) => ({
    ...state,
    monthPainLogs: data
  }),
  [GET_LASTEST_EXPIRED_PLAN.SUCCESS]: (state, { data }) => ({
    ...state,
    reportData: data
  }),
  [CREATE_PAIN_LOG.SUCCESS]: (state, { result, overallRating }) => {
    const prePainLogs = state.painLogs || []
    const { PainLogId, HeadPainSites, PainSites } = result
    let nextPainLogs = []
    if (overallRating === 0) {
      nextPainLogs = [{ ...result, OverallRating: 0 }]
      if (prePainLogs.length > 0 && prePainLogs[0].OverallRating !== 0) return state
    } else {
      const editIndex = prePainLogs.findIndex(item => item.PainLogId === PainLogId)
      const HeadPain = Array.isArray(HeadPainSites) && HeadPainSites.length > 0
      // @ts-ignore
      const bodyPainSites = PainSites?.map(item => item.BodyMap)
      const nextItem = { ...result, OverallRating: overallRating, HeadPain, BodyPainSites: bodyPainSites }
      const noPainIndex = prePainLogs.findIndex(item => item.OverallRating === 0)
      if (editIndex > -1) {
        nextPainLogs = [...prePainLogs]
        nextPainLogs[editIndex] = nextItem
      } else {
        nextPainLogs = [...prePainLogs, nextItem]
      }
      if (noPainIndex > -1) {
        nextPainLogs.splice(noPainIndex, 1)
      }
    }
    return ({
      ...state,
      painLogs: nextPainLogs
    })
  },
  [LOGOUT_USER.SUCCESS]: state => ({ ...state,
    todayStepRecord: null,
    todayPainLog: null,
    reportData: null,
    monthPainLogs: [],
    painLogs: []
  }),
})
